#!/usr/bin/python

from __future__ import division, print_function
from adder import Adder
import math
import sys

class Executor():
	def execute(self):
		interpreter = Adder();
		interpreter.engine.before_error = "X\n"
		
		try:
			filename = sys.argv[1]
		except:
			filename = raw_input("Please enter a BITE filename: ")
		
		try:
			sys.stdout.write("Reading file... ")
			f = open(filename)
			bytecode = f.read()
			f.close()
			sys.stdout.write("done.\n")
		except IOError:
			print("Invalid filename entered.")
			return
		
		# Try and load all of the object data
		sys.stdout.write("Loading bytecode... ")
		bytecode, functions, classes, imports = interpreter.loadBytecode(bytecode.split("\n"))
		sys.stdout.write("done.\n")
		
		# imports are not loaded in Python executor.
		for i in imports:
			print("WARN: Imports are not supported in Python-based bytecode executor.  Import " + i + " will not be loaded!")
		
		# Now execute.
		print("Executing main bytecode: ")
		print("============================================")
		
		interpreter.executeBytecode(bytecode, functions, classes, imports)
	
print("Welcome to the Adder Executor.")
com = Executor()
com.execute()